package com.ruoyi.energy.service.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.energy.domain.vo.NyMeterAccountVO;
import com.ruoyi.energy.mapper.NyCheckMeterMapper;
import com.ruoyi.energy.mapper.NyMeterAccountMapper;
import com.ruoyi.energy.service.impl.NyCheckMeterServiceImpl;
import com.ruoyi.omp.api.domain.NyCheckMeter;
import com.ruoyi.omp.api.domain.vo.NyCheckMeterVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 参考菜单
 */
@Service
public class NyMeterAccountManager {
    private static final Logger log = LoggerFactory.getLogger(NyMeterAccountManager.class);

    @Autowired
    private NyMeterAccountMapper nyMeterAccountMapper;

    @Autowired
    private NyCheckMeterMapper nyCheckMeterMapper;

    //构建树
    public NyMeterAccountVO selectRoot(String id) {
        NyMeterAccountVO result = nyMeterAccountMapper.selectRoot(id);
        if (result == null) {
            return result;
        }
        fillChildren(result);
        return result;
    }

    private void fillChildren(NyMeterAccountVO node) {
        List<NyMeterAccountVO> result = nyMeterAccountMapper.selectByParentId(node.getId());
        if(result == null){
            return ;
        }
        if(result.size() == 0){
            return ;
        }

        for(NyMeterAccountVO nyMeterAccountVO:result ){
            if(nyMeterAccountVO.getId().equals(nyMeterAccountVO.getParentId())){
                continue;
            }
            if(nyMeterAccountVO.getCategory().equals("考核表")|| nyMeterAccountVO.getCategory().equals("总表")){
                fillChildren(nyMeterAccountVO);
            }else {
                continue;
            }
        }
        node.setChildren(result);

    }
    // 获取树私表叶子节点
    public List<NyMeterAccountVO> selectPrivateLeafs(NyMeterAccountVO parent) {
        List<NyMeterAccountVO> leafs = new ArrayList();
        fillLeaf(parent,leafs);
        return leafs;
    }

    private void fillLeaf(NyMeterAccountVO parent, List<NyMeterAccountVO> leafs) {
        //叶子节点，私表添加，
        if(parent.getChildren().size() ==0){
            if(parent.getCategory().equals("私表")){
                leafs.add(parent);
            }
            return;
        }
        for(NyMeterAccountVO node :parent.getChildren()){
            if(node.getCategory().equals("总表")||node.getCategory().equals("考核表")){
                fillLeaf(node,leafs);
            }else {
                leafs.add(node);
            }
        }

    }
    //树遍历

    //构建抄表记录树
    public NyCheckMeterVO selectCheckMeterTree(NyMeterAccountVO nyMeterAccountVO, Date feeCycle){
        NyCheckMeterVO result = new NyCheckMeterVO();
        result.setFeeCycle(feeCycle);
        if(nyMeterAccountVO != null){
            copyBeanCheckVO1(nyMeterAccountVO,result);
        }
        return result;
    }

    //拷贝抄表记录树
    private void copyBeanCheckVO1(NyMeterAccountVO nyMeterAccountVO,NyCheckMeterVO result){
        //遍历仪表树节点，根据树节点查询构建抄表记录
        //查询抄表记录
        NyCheckMeter nyCheckMeter = new NyCheckMeter();
        nyCheckMeter.setMeterNum(nyMeterAccountVO.getMeterNum());
        nyCheckMeter.setFeeCycle(result.getFeeCycle());
        nyCheckMeter.setMeterId(nyMeterAccountVO.getId());
        NyCheckMeter sumNyCheckMeter = nyCheckMeterMapper.selectNyCheckMeterVO(nyCheckMeter);
        if(sumNyCheckMeter != null ){
            BeanUtils.copyBeanProp(result,sumNyCheckMeter);
            result.setMeterModel(nyMeterAccountVO.getMeterModel());
            //BeanUtil.copyProperties(sumNyCheckMeter,result, CopyOptions.create().setIgnoreProperties("children"));
            //result
        }else{
            //copyBeanProp(result,nyMeterAccountVO);
            BeanUtil.copyProperties(nyMeterAccountVO,result, CopyOptions.create().setIgnoreProperties("children"));
            result.setMeterId(nyMeterAccountVO.getId());
            log.error(result.getMeterNum() + ",feeCycle: " + result.getFeeCycle() + "is null");
        }
        if(nyMeterAccountVO.getCategory().equals("总表")||nyMeterAccountVO.getCategory().equals("考核表")){
            List<NyMeterAccountVO> children = nyMeterAccountVO.getChildren();
            if(children.size()>0){
                for(NyMeterAccountVO child:children){
                    NyCheckMeterVO checkMeterVO = new NyCheckMeterVO();
                    checkMeterVO.setFeeCycle(result.getFeeCycle());
                    result.getChildren().add(checkMeterVO);
                    if(child != null){
                        copyBeanCheckVO1(child,checkMeterVO);
                    }
                }
            }else{
                return;
            }
        }else{
            return;
        }
    }

}
